<?php
namespace api\service;

use api\service\interfaces\IQuoteService;
use herosphp\core\Loader;
use herosphp\core\Log;
use herosphp\utils\AjaxResult;
use common\service\CommonService;
use herosphp\bean\Beans;

Loader::import('api.service.interfaces.IQuoteService');

/**
 * quote行情 api(Service)接口实现
 * @author yu
 */
class QuoteService extends CommonService implements IQuoteService
{


    public function __construct()
    {
        $this->thisModel = Loader::model('quoteData');
    }

    //用户获取牌号数据统计
    public function searchByUserBrandStatistics($pagesize, $page, $search_arr)
    {
        $offset = $pagesize * ($page - 1); //计算记录偏移量

        $count_sql = "Select count(*)  From user_search_brand_record";

        $total = $this->thisModel->getDB()->excute($count_sql)->fetchColumn();

        //分页查询
        $pageinationSql = isset ($offset) ? " Limit $offset,$pagesize" : " Limit 0,$pagesize";

        $sql = "Select *  From user_search_brand_record Order By updatetime Desc $pageinationSql";

        $items = $this->thisModel->getDB()->excute($sql)->fetchAll($fetch_style = 2);

        return new AjaxResult(AjaxResult::OP_SUCCESS, "loading successfully", array("items" => $items, "total" => $total));

    }


    //用户获取品名数据统计
    public function searchByUserProductStatistics($pagesize, $page, $search_arr)
    {
        $offset = $pagesize * ($page - 1); //计算记录偏移量

        $count_sql = "Select count(*)  From user_search_product_record";

        $total = $this->thisModel->getDB()->excute($count_sql)->fetchColumn();

        //分页查询
        $pageinationSql = isset ($offset) ? " Limit $offset,$pagesize" : " Limit 0,$pagesize";

        $sql = "Select *  From user_search_product_record Order By updatetime Desc $pageinationSql";

        $items = $this->thisModel->getDB()->excute($sql)->fetchAll($fetch_style = 2);

        return new AjaxResult(AjaxResult::OP_SUCCESS, "loading successfully", array("items" => $items, "total" => $total));

    }

    //行情趋势图数据
    public function showTrendData($search_arr)
    {


        $quoteDataService = Beans::get('quote.data.service');

        $result = $quoteDataService->getList(1000, 1, $search_arr);

        $result['trendChartData'] = $quoteDataService->getChartData($result['items'], $search_arr['addtime_s'], $search_arr['addtime_e']);

        return new AjaxResult(AjaxResult::OP_SUCCESS, "loading successfully", $result);

    }
    //显示行情详情 ,供大易门户－》行情资讯－》现货行情调用
    public function showQuote($product_name,$date)
    {

        $quoteService = Beans::get('quote.quote.service');

        $result = $quoteService->getProduct($product_name, $date);

        return new AjaxResult(AjaxResult::OP_SUCCESS, "loading successfully", array("items" => $result, "total" => $result['total']));

    }

    //行情中心数据,供大易门户－》行情资讯－》现货行情调用和Saas中心调用
    public function showQuoteList($pagesize, $page, $search_arr)
    {

        $quoteAnalysisService = Beans::get('quote.analysis.service');
        $quoteService = Beans::get('quote.quote.service');

        $result = $quoteAnalysisService->getList($pagesize, $page, $search_arr);

        foreach ($result['items'] as $k => $item) {
            $result['items'][$k]['title'] = $quoteService->getQuoteTitle($item['adddate'], $item['product_name']);
        }

        return new AjaxResult(AjaxResult::OP_SUCCESS, "loading successfully", array("items" => $result['items'], "total" => $result['total']));

    }

    //行情数据列表
    public function showQuoteDataList( $pagesize, $page, $search_arr ){

        $quoteDataService = Beans::get('quote.data.service');

        $result = $quoteDataService -> getDataList(  $search_arr,"addtime desc",array($page, $pagesize) );

        return new AjaxResult(AjaxResult::OP_SUCCESS, "loading successfully", array("items" => $result['items'], "total" => $result['total']));

    }
}
